package com.base.llpark;


import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.arvin.imagepicker.AndroidImagePicker;
import com.arvin.imagepicker.bean.ImageItem;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.utils.RxBus2;
import com.base.llpark.bean.IllegalPark;
import com.base.llpark.bean.LlPark;
import com.base.llpark.bean.ParkDetail;
import com.base.llpark.databinding.ActivityParkdetailBinding;
import com.base.llpark.presenter.ParkDetailPresenter;
import com.base.llpark.utills.CrpuctPrintUtil;
import com.base.llpark.view.ParkDetailView;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by arvin on 2018/11/7.
 */
@Route(path = "/lpark/parkdetail")
public class ParkDetailActivity extends CBaseBarMvpActivity<ParkDetailPresenter> implements ParkDetailView {
    protected CompositeDisposable mCompositeDisposable=new CompositeDisposable();

    private ActivityParkdetailBinding mBinding;
    @Autowired(name = "mPark")
    IllegalPark park;
    @Autowired
    int mType;

    private LlPark mPark = null;
    private String CarTypeName = "";

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityParkdetailBinding) mRootBinding;
        mPresenter = new ParkDetailPresenter(this,this);
        mPresenter.start();
        setBackNavigation();
        setBarTitle("违停详情");
        getFile(park.getId());
    }

    @Override
    protected void start() {
        if (mType ==1){
            mBinding.parkSett.setVisibility(View.VISIBLE);
        }
        mBinding.btnPrint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mPark != null) {
                    if (RealNetWorkUtil.TYPE.equals("0")){
                        CrpuctPrintUtil.printPark(mContext, mPark, CarTypeName);
                    }else if (RealNetWorkUtil.TYPE.equals("1")){
                        CrpuctPrintUtil.printParkKS(mContext, mPark, CarTypeName);
                    }
                }else {
                    showMes("数据异常，请重新获取");
                }
            }
        });
        mBinding.btnUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mPark != null) {
                    mPresenter.ReportLLpark(mPark);
                }else {
                    showMes("数据异常，请重新获取");
                }
            }
        });

    }
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_parkdetail;
    }
    private void getFile(String id)
    {
        Disposable enqueue = RealNetWorkUtil.get().method("api-ll/illegal?id="+id).params(new HashMap<String, Object>())
                .build().enqueue(ParkDetail.class, new CallBackImpl<ParkDetail>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        showError();
                    }

                    @Override
                    public void onNext(ParkDetail list) {
                        mBinding.setEntity(list);
                        mPark = new LlPark();
                        mPark.setId(list.getId());
                        if (mType == 1){
                            mPark.setState("未上报");
                        }
                        mPark.setState(TextUtils.isEmpty(list.getState())?"0":list.getState());
                        mPark.setCheckDate(list.getCheckDate());
                        mPark.setCarNo(list.getCarNo());
                        mPark.setNoticeNo(list.getNoticeNo());
                        mPark.setCheckSignAddress(list.getCheckSignAddress());
                        mPark.setUserName(list.getPersonName1());
                        mPark.setAddress(mPresenter.mUser.getAddress());
                        mPark.setPhone(mPresenter.mUser.getPhone());
                        mPark.setCompanyName(mPresenter.mUser.getCompanyName());
                        mPark.setPersonName2(list.getPersonName2());
                        CarTypeName = list.getCarTypeName();
                        List<ImageItem> items = new ArrayList<>();
                        ImageItem item = null;
                        for (String path : list.getFiles()) {
                            item = new ImageItem(RealNetWorkUtil.getBaseUrl()+"/api-file/"+path,"");
                            items.add(item);
                        }
                        mBinding.pick.setPics(items);
                    }
                });
        mCompositeDisposable.add(enqueue);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Glide.get(this).clearMemory();
        AndroidImagePicker.getInstance().onDestroy();
        mCompositeDisposable.dispose();
    }

    @Override
    public void returnReportSuccess() {
        RxBus2.getInstance().post(new ParkDetail());
        finish();
    }
}
